package com.xinjian.jianba.service.impl;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xinjian.jianba.dao.dwmid.MomCommentDao;
import com.xinjian.jianba.dto.MomCommentDto;
import com.xinjian.jianba.dto.MomCommentReplayDto;
import com.xinjian.jianba.dto.MomcommentsVo;
import com.xinjian.jianba.dto.MonCommentsDto;
import com.xinjian.jianba.enity.MomCommentEntity;
import com.xinjian.jianba.service.MomCommentService;
import com.xinjian.jianba.utils.TimeUtil;
import com.xinjian.jianba.utils.Utils;
@Service
public class MomCommentServiceImpl implements MomCommentService {

	@Autowired MomCommentDao momCommentDao;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Long saveMomComment(MomCommentDto momCommentDto) {
		java.util.Date date = new java.util.Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		
		Long userId=Long.parseLong(momCommentDto.getUserId().trim());
		Long pubUserId=Long.parseLong(momCommentDto.getPubUserId().trim());
		MomCommentEntity momCommentEntity=new MomCommentEntity();
		momCommentEntity.setAddTime(timestamp);
		momCommentEntity.setTimeAdd(new java.sql.Date(date.getTime()));
		momCommentEntity.setDetail(momCommentDto.getDetail());
		momCommentEntity.setMonId(Long.parseLong(momCommentDto.getMonId()));
		momCommentEntity.setUserId(userId);
		momCommentEntity.setPubUserId(pubUserId);
		if(userId.equals(pubUserId)){
			momCommentEntity.setStatus(1l);
		}else{
			momCommentEntity.setStatus(0l);
		}
		momCommentEntity.setTargetUserId(0l);
		momCommentEntity.setTargetUserName("");
		momCommentDao.insert(momCommentEntity);
		Long comId=momCommentEntity.getComId();
		return comId;
	}
	@Override
	public Long saveMomCommentReplay(MomCommentReplayDto momCommentReplayDto) {
		java.util.Date date = new java.util.Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		Long userId=Long.parseLong(momCommentReplayDto.getUserId().trim());
		Long targetUserId=Long.parseLong(momCommentReplayDto.getTargetUserId().trim());
		MomCommentEntity momCommentEntity=new MomCommentEntity();
		momCommentEntity.setAddTime(timestamp);
		momCommentEntity.setTimeAdd(new java.sql.Date(date.getTime()));
		momCommentEntity.setDetail(momCommentReplayDto.getDetail());
		momCommentEntity.setMonId(Long.parseLong(momCommentReplayDto.getMonId()));
		momCommentEntity.setUserId(userId);
		momCommentEntity.setPubUserId(Long.parseLong(momCommentReplayDto.getPubUserId()));
		if(userId.equals(targetUserId)){
			momCommentEntity.setStatus(1l);
		}else{
			momCommentEntity.setStatus(0l);
		}
		momCommentEntity.setTargetUserId(targetUserId);
		momCommentDao.insert(momCommentEntity);
		Long comId=momCommentEntity.getComId();
		return comId;
	}
	@Override
	public Map<String,Object> findCommentAndReplayByMonId(MonCommentsDto monCommentsDto) throws ParseException {
		List<MomCommentEntity> list=momCommentDao.findCommentAndReplayByMonId(monCommentsDto);
		List<MomcommentsVo> reList=new ArrayList<MomcommentsVo>();
		for(MomCommentEntity momCommentEntity:list){
			MomcommentsVo momcommentsVo=new MomcommentsVo();
			momcommentsVo.setAddTime(TimeUtil.getTimeFormatText(Utils.convertTimestampToString(momCommentEntity.getAddTime())));
			momcommentsVo.setDetail(momCommentEntity.getDetail()==null?"":momCommentEntity.getDetail());
			momcommentsVo.setHeadAdress(momCommentEntity.getHeadAdress()==null?"":momCommentEntity.getHeadAdress());
			momcommentsVo.setTargetUserId(momCommentEntity.getTargetUserId());
			momcommentsVo.setTargetUserName(momCommentEntity.getTargetUserName()==null?"":momCommentEntity.getTargetUserName());
			momcommentsVo.setUserId(momCommentEntity.getUserId());
			momcommentsVo.setUserName(momCommentEntity.getUserName()==null?"":momCommentEntity.getUserName());
			momcommentsVo.setComId(momCommentEntity.getComId());
			reList.add(momcommentsVo);
		}
		Map<String,Object> map=new HashMap<String,Object>();
		long count=momCommentDao.findCommentCountsByMonId(monCommentsDto.getMonId());
		map.put("result", reList);
		map.put("count", count+"");
		return map;
	}
	@Override
	public int deleteMomcomment(Map<String, Object> map) {
		int re=momCommentDao.deleteMomcomment(map);
		return re;
	}

}
